package com.example.sunran.servlet;

import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class GetFileListServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");

        File file = null;
        if (type.equals("video")) {
            file = new File("./UploadFile/Video/");
        }else if(type.equals("image")){
            file = new File("./UploadFile/Image/");
        }else if(type.equals("doc")){
            file = new File("./UploadFile/Doc/");
        }else if(type.equals("other")){
            file = new File("./UploadFile/Other/");
        }

        String[] listFiles = file.list();

        JSONArray jsonArray = new JSONArray();
        for(String fileName:listFiles){
            jsonArray.put(fileName);
        }

        resp.setStatus(200);
        resp.setContentType("text/html;charset=utf-8");
        resp.getOutputStream().write(jsonArray.toString().getBytes());
    }
}
